package com.myrecycleveiw;

import android.content.Context;
import android.content.res.TypedArray;
import android.support.annotation.Nullable;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;
import android.util.Log;
import android.view.ViewGroup;
import android.widget.LinearLayout;

/**
 * 上啦加载下拉刷新 RecycleView
 * Created by 黄海 on 2017/4/20.
 */

public class ExpandRecycleView extends LinearLayout implements SwipeRefreshLayout.OnRefreshListener {
    String TAG = "MyRecycleView";
    private SwipeRefreshLayout swipeRefreshLayout;
    private RecyclerView recycleView;
    private FooterType footerType = new FooterType();
    private boolean isLoading;
    private WrapperAdapter adapter;
    private OnLoadMoreListener onLoadMoreListener;
    private SwipeRefreshLayout.OnRefreshListener onRefreshListener;
    private boolean enableRefresh, enableLoadMore;

    public ExpandRecycleView(Context context) {
        this(context, null);
    }

    public ExpandRecycleView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public ExpandRecycleView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        final TypedArray a = context.obtainStyledAttributes(
                attrs, R.styleable.ExpandRecycleView, defStyleAttr, 0);
        enableLoadMore = a.getBoolean(R.styleable.ExpandRecycleView_enable_loadmore, false);
        enableRefresh = a.getBoolean(R.styleable.ExpandRecycleView_enable_refresh, false);
        a.recycle();
        swipeRefreshLayout = new SwipeRefreshLayout(context);
        swipeRefreshLayout.setOnRefreshListener(this);
        swipeRefreshLayout.setEnabled(enableRefresh);
        recycleView = new RecyclerView(context);
        recycleView.setLayoutManager(new LinearLayoutManager(context));
        swipeRefreshLayout.addView(recycleView, new ViewGroup.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
        addView(swipeRefreshLayout, new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
        recycleView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                LinearLayoutManager layoutManager = (LinearLayoutManager) recyclerView.getLayoutManager();
                int lastVisibleItemPosition = layoutManager.findLastVisibleItemPosition();
                if (enableLoadMore && adapter != null) {
                    if (lastVisibleItemPosition + 1 == adapter.getItemCount()) {
                        Log.e(TAG, "onScrolled: footerType=" + footerType.getType().name());
                        if (footerType.getType() == FooterType.Type.LoadMore && !isLoading && onLoadMoreListener != null) {
                            onLoadMore();
                        }
                    }
                }
            }
        });
    }


    public void setAdapter(RecyclerView.Adapter adapter) {
        recycleView.setAdapter(this.adapter = new WrapperAdapter(adapter, footerType, this));
    }

    public void setLoadMoreCompleted() {
        footerType.setType(FooterType.Type.LoadMore);
        isLoading = false;
        adapter.notifyItemRemoved(adapter.getItemCount() - 1);
    }

    public void setLoadNoMore() {
        footerType.setType(FooterType.Type.NoMore);
        isLoading = false;
        adapter.notifyItemRemoved(adapter.getItemCount() - 1);
    }

    public void setLoadNetWorkError() {
        footerType.setType(FooterType.Type.NetWorkError);
        isLoading = false;
        adapter.notifyItemRemoved(adapter.getItemCount() - 1);
    }

    public void setOnLoadMoreListener(OnLoadMoreListener onLoadMoreListener) {
        this.onLoadMoreListener = onLoadMoreListener;
    }

    public void setSwipeRefreshLayout(SwipeRefreshLayout swipeRefreshLayout) {
        this.swipeRefreshLayout = swipeRefreshLayout;
    }

    public WrapperAdapter getAdapter() {
        return adapter;
    }

    @Override
    public void onRefresh() {
        if (onRefreshListener != null) {
            onRefreshListener.onRefresh();
        } else {
            swipeRefreshLayout.setRefreshing(false);
        }
    }

    protected void onLoadMore() {
        isLoading = true;
        if (onLoadMoreListener != null) {
            onLoadMoreListener.onLoadMore();
        }
    }
}
